package com.atguigu.tingshu.order.service.impl;

import com.atguigu.tingshu.account.AccountFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.order.OrderDetail;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.mapper.OrderDetailMapper;
import com.atguigu.tingshu.order.mapper.OrderInfoMapper;
import com.atguigu.tingshu.order.service.AccountDeductService;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.account.AccountDeductVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: jack
 * @date: 2025/10/13 19:08
 */
@Service
public class AccountDeductServiceImpl implements AccountDeductService {

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private AccountFeignClient accountFeignClient;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private OrderInfoMapper orderInfoMapper;


    @Override
    public void handleAccountDeduct(OrderInfo orderInfo) {
        AccountDeductVo accountDeductVo = new AccountDeductVo();
        accountDeductVo.setOrderNo(orderInfo.getOrderNo());
        accountDeductVo.setUserId(orderInfo.getUserId());
        accountDeductVo.setAmount(orderInfo.getOrderAmount());
        accountDeductVo.setContent(orderInfo.getOrderTitle());

        Result result = accountFeignClient.checkAndeduct(accountDeductVo);
        if (result.getCode().intValue() != 200) {
            throw new GuiguException(result.getCode(), result.getMessage());
        }
        orderInfo.setOrderStatus(SystemConstant.ORDER_STATUS_PAID);
        orderInfoMapper.updateById(orderInfo);


        UserPaidRecordVo userPaidRecordVo = new UserPaidRecordVo();
        List<OrderDetail> orderDetailList = orderDetailMapper.selectList(
                new LambdaQueryWrapper<>(OrderDetail.class)
                        .eq(OrderDetail::getOrderId, orderInfo.getId())
                        .select(OrderDetail::getItemId)
        );
        if (CollectionUtils.isNotEmpty(orderDetailList)) {
            List<Long> longs = orderDetailList.stream().map(OrderDetail::getItemId).collect(Collectors.toList());
            userPaidRecordVo.setItemIdList(longs);
        }
        userPaidRecordVo.setOrderNo(orderInfo.getOrderNo());
        userPaidRecordVo.setUserId(orderInfo.getUserId());
        userPaidRecordVo.setItemType(orderInfo.getItemType());
        Result result1 = userFeignClient.savePaidRecord(userPaidRecordVo);
        if (result1.getCode().intValue() != 200) {
            throw new GuiguException(result1.getCode(), result1.getMessage());
        }
    }
}
